package com.bruce.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bruce.dao.RoleDao;
import com.bruce.dao.RolePermissionDao;
import com.bruce.model.Role;
import com.bruce.model.RolePermission;
import com.bruce.model.UserRole;
import com.bruce.service.RolePermissionService;
import com.bruce.service.RoleService;
import com.bruce.service.UserRoleService;

@RestController
@RequestMapping("/manager/role")
public class RoleController {
	@Autowired
	private RoleService roleServiceImpl;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private RolePermissionService rolePermissionService;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RolePermissionDao rolePermissionDao;
	
	@PostMapping("/add")
	public Map<String, String> add( @RequestParam("name") String name, @RequestParam("type") String type ){
		Role impl = new Role();
		impl.setName(name);
		impl.setType(type);
		Map<String, String> reMap = new HashMap<String, String>();
		try {
			reMap.put("status", String.valueOf(roleServiceImpl.insert(impl)) );
			reMap.put("roleid", String.valueOf(roleServiceImpl.getId(impl)) );
		}catch(Exception e) {
			reMap.put("status", "0" );
		}
		return reMap;
	}

	@PostMapping("/list")
	public Object findAll(
			@RequestParam(name="pgeNum", required=false, defaultValue="1") int pageNum,
			@RequestParam(name="pageSize", required= false, defaultValue = "10")int pageSize) {
		return roleServiceImpl.selectAll(pageNum,pageSize);

	}
	@PostMapping("/listNoPage")
	public Map<String, Object> findAllNoPage() {
		Map<String, Object> reMap = new HashMap<String, Object>();
		reMap.put("reList", roleDao.selectAll());
		return reMap;
	}

	@PostMapping("/update")
	public Map<String, String> up(@RequestParam("id") Integer id,@RequestParam("name") String name, @RequestParam("type") String type ){
		Role impl = new Role();
		impl.setId(id);
		impl.setName(name);
		impl.setType(type);
		Map<String, String> reMap = new HashMap<String, String>();
		try {
			reMap.put("status", String.valueOf(roleServiceImpl.update(impl)) );
		}catch(Exception e) {
			reMap.put("status", "0" );
		}
		return reMap;
	}
	
	@PostMapping("/userUpRole")
	public Map<String, String> userUpRole( @RequestParam("uid") Integer uid, @RequestParam("rid") Integer rid ){
		UserRole impl = new UserRole();
		impl.setUid(uid);
		impl.setRid(rid);
		Map<String, String> reMap = new HashMap<String, String>();
		try {
			boolean falg = userRoleService.isUser(uid);
			if(falg) {
				reMap.put("status", String.valueOf(userRoleService.update(impl)) );
			}else {
				reMap.put("status", String.valueOf(userRoleService.insert(impl)) );
			}
		}catch(Exception e) {
			e.printStackTrace();
			reMap.put("status", "0" );
		}
		return reMap;
	}

	@PostMapping("/rolesByUser")
	public Object rolesByUser( @RequestParam(name="uid") int uid) {
		return userRoleService.getRoleByRole(uid);

	}

	@PostMapping("/delRolesByUser")
	public Map<String, String> delRolesByUser(@RequestParam(name="uid") int uid, @RequestParam(name="rid") int rid){
		Map<String, String> reMap = new HashMap<String, String>();
		try {
			reMap.put("status", String.valueOf(userRoleService.delete(uid, rid)) );
		}catch(Exception e) {
			e.printStackTrace();
			reMap.put("status", "0" );
		}
		return reMap;
	}
	
	@PostMapping("/roleAddPermission")
	public Map<String, String> roleAddPermission( @RequestParam("pid") Integer pid, @RequestParam("rid") Integer rid ){
		RolePermission impl = new RolePermission();
		impl.setPid(pid);
		impl.setRid(rid);
		Map<String, String> reMap = new HashMap<String, String>();
		try {
				reMap.put("status", String.valueOf(rolePermissionService.insert(impl)) );
		}catch(Exception e) {
			reMap.put("status", "0" );
		}
		return reMap;
	}

	@PostMapping("/permissionsByRid")
	public Object permissionsByRid( @RequestParam(name="rid") int rid,
			@RequestParam(name="pgeNum", required=false, defaultValue="1") int pageNum,
			@RequestParam(name="pageSize", required= false, defaultValue = "10")int pageSize) {
		return rolePermissionService.permissionsByRid(rid, pageNum, pageSize);

	}

	
	@PostMapping("/permissionsByRidNoPage")
	public Object permissionsByRid( @RequestParam(name="rid") int rid) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		reMap.put("reList",rolePermissionDao.permissionsByRid(rid));
		return reMap;

	}
	
	
	@PostMapping("/delPermissionByRid")
	public Map<String, String> delPermissionByRid(@RequestParam(name="pid") int pid, @RequestParam(name="rid") int rid){
		Map<String, String> reMap = new HashMap<String, String>();
		try {
			reMap.put("status", String.valueOf(rolePermissionService.delete(rid, rid)) );
		}catch(Exception e) {
			reMap.put("status", "0" );
		}
		return reMap;
	}
	
	@PostMapping("/upPermissionsByRid")
	public Map<String, String> upPermissionsByRid( @RequestParam(name="pidstr") String pidstr, @RequestParam(name="rid") int rid ) {
		Map<String, String> reMap = new HashMap<String, String>();
		String [] pids = pidstr.split(",");
		try {
			rolePermissionService.deleteByRid(rid);
			for(int i=0 ; i<pids.length; i++) {
				roleAddPermission(Integer.valueOf(pids[i]), rid);
			}
			reMap.put("status", "1");
		}catch(Exception e) {
			reMap.put("status", "0" );
		}
		return reMap;

	}
}
